<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-title">智慧大棚数字化追溯平台</div>
      <el-form
        :model="formData"
        ref="formRef"
        :rules="rules"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="formData.username"
            placeholder="请输入用户名"
            :prefix-icon="User"
            clearable
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="formData.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            show-password
          />
        </el-form-item>

        <div class="button-group">
          <el-button
            type="primary"
            :loading="loading"
            @click="handleSubmit('login')"
          >
            立即登录
          </el-button>
          <el-button
            @click="handleSubmit('register')"
            :loading="loading"
          >
            注册账号
          </el-button>
        </div>
      </el-form>

      <div class="demo-account">
        <p>测试账号：555</p>
        <p>测试密码：123456</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import axios from 'axios'

const router = useRouter()
const formRef = ref(null)
const loading = ref(false)

const formData = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ]
}

const handleSubmit = async (type) => {
  if (!formRef.value) return

  try {
    await formRef.value.validate()
    loading.value = true

    if (type === 'login') {
      const response = await axios.post('/user/login', formData)
      if (response.data) {
        localStorage.setItem('user', JSON.stringify(response.data.user))
        localStorage.setItem('sensorToken', response.data.sensorToken)
        localStorage.setItem('cameraToken', response.data.cameraToken)
        ElMessage.success('登录成功')
        router.push('/')
      }
    } else {
      const response = await axios.post('/user/register', formData)
      if (response.data) {
        ElMessage.success('注册成功，请登录')
        formRef.value.resetFields()
      }
    }
  } catch (error) {
    ElMessage.error(error.response?.data || `${type === 'login' ? '登录' : '注册'}失败`)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  background: linear-gradient(135deg, #1a237e 0%, #0d47a1 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
  background-image:url("../assets/OIP-C.jpg") ;
}

.login-box {
  position: relative;
  width: 420px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.login-title {
  color: #fff;
  font-size: 24px;
  text-align: center;
  margin-bottom: 40px;
  font-weight: bold;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.login-form {
  margin-bottom: 20px;
}

:deep(.el-input) {
  --el-input-bg-color: rgba(255, 255, 255, 0.1);
  --el-input-text-color: #fff;
  --el-input-border-color: rgba(255, 255, 255, 0.2);
  --el-input-hover-border-color: #409eff;
}

:deep(.el-input__wrapper) {
  box-shadow: none !important;
  background: rgba(255, 255, 255, 0.1);
}

:deep(.el-input__inner) {
  color: #fff !important;
}

:deep(.el-input__inner::placeholder) {
  color: rgba(255, 255, 255, 0.6);
}

.button-group {
  display: flex;
  gap: 15px;
  margin-top: 30px;
}

.button-group .el-button {
  flex: 1;
  height: 40px;
  font-size: 16px;
}

.demo-account {
  margin-top: 20px;
  text-align: center;
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
}

.demo-account p {
  margin: 5px 0;
}

/* 添加动画效果 */
.login-box {
  animation: fadeIn 0.5s ease-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style> 